#! /usr/bin/env python
import commands
import genbits
def createtable(filename, arguments):
    f = open (filename, 'w')

    totalbits = map(lambda x: 2**x, range(3,20))
    onebits = [0] + map(lambda x: 2**x, range(0,19))
    for x in totalbits:
        for y in onebits:
            if (y <= x/2):
                filen1 = 'createtabletemp.tbits'
                genbits.genbits(x-y, y, filen1)
                filen2 = ' createtabletemp.bits'
                cmd2 = 'arithencodebits ' + arguments + ' ' + filen1 + ' ' + filen2
                print cmd2
                result = commands.getoutput(cmd2);
                print result
                f.write(str(x) + ' ' + str(y) + ' ' + result.splitlines()[-1] + '\n')
    f.close()
    commands.getoutput('rm createtabletemp.tbits createtabletemp.bits')

def spaceconcat(x,y): return x + ' ' + y

if __name__ == "__main__":
    import sys
#   print(sys.argv)
#   sys.argv should pass in arguments to arithencodebits and arithdecodebits. 
    if len(sys.argv) <= 1:
        print "Usage: python ",sys.argv[0], " tablefilename [arguments passed to arithencodebits and arithdecodebits]" 
    else:
        createtable(sys.argv[1],reduce(spaceconcat,  sys.argv[2:]))

        
